

var _DB_ = require('./initDB');
var uuid = require('uuid');
var DB = new _DB_();

var tools = require('./tools');


var makeError = error => {
    return {
        type: 'error',
        message: error,
        res: null,
    }
}

module.exports = {
    service: {
        // 创建服务器信息
        creatService: async data => {
            var _id = uuid.v1();
            var _create_time = new Date().getTime();
            var sql = `INSERT INTO service (
                service_id, 
                title, 
                ip, 
                username, 
                password, 
                update_time, 
                create_time
            ) VALUES (
                 '${_id}',
                 '${data.title}',
                 '${data.ip}',
                 '${data.username}',
                 '${data.password}'
                 ,${_create_time},
                 ${_create_time}
            ) `;
            try {
                await DB.creat(sql);
                data.serviceId = _id;
                data.updateTime = _create_time;
                data.createTime = _create_time;
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            }
        },
        // 获取连接过得服务器信息
        getService: async id => {
            try {
                var data = await DB.get(`SELECT * FROM service WHERE service_id = '${id}'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        getAllService: async () => {
            try {
                var data = await DB.find(`SELECT * FROM service`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            };
        },
        updateService: async (data) => {
            var _update_time = new Date().getTime();
            var sql = `UPDATE service SET 
                        title = '${data.title}',
                        ip = '${data.ip}', 
                        username = '${data.username}', 
                        password = '${data.password}', 
                        update_time = ${_update_time}
                        WHERE service_id = '${data.serviceId}';`;
            try {
                await DB.update(sql);
                data.updateTime = _update_time;
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            }
        }
    },
    user: {
        getMainUser: async () => {
            try {
                var data = await DB.get(`SELECT * FROM user WHERE user_id = 'Local'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        updateMainUser: async (data) => {
            var Sql = `UPDATE user SET 
                            nickname = '${data.nickname}',
                            avater = '${data.avater}'
                            WHERE user_id = 'Local'`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            };
        },
    },
    api: {
        getRenderData: async (id) => {
            var folderSql = `SELECT * FROM folder WHERE folder_project_id = '${id}' and disable = 0 and type = 0`;
            var apiSql = `SELECT * FROM project_api WHERE project_id = '${id}' and disable = 0`;
            try {
                var folderList = await DB.find(folderSql);
                var apiList = await DB.find(apiSql);
                for (let i = 0; i < folderList.length; i++) {
                    const folderItem = folderList[i];
                    folderItem.id = folderItem.folderId;
                };
                for (let i = 0; i < apiList.length; i++) {
                    const api = apiList[i];
                    api.id = api.apiId;
                    api.apiFolderId = api.folderId;
                };
                return {
                    type: 'success',
                    res: {
                        folderList: folderList,
                        apiList: apiList,
                    },
                };
            } catch (error) {
                return makeError(error);
            };
        },
        creatFolder: async (newFolder, parentFolder) => {
            var _id = uuid.v1();
            var _create_time = new Date().getTime();
            var sql = `INSERT INTO folder (
                folder_id,
                folder_project_id,
                folder_parent_id,
                title,
                description,
                type,
                disable,
                update_time, 
                create_time
                ) VALUES (
                '${_id}',
                '${newFolder.projectId}',
                '${parentFolder.folderId}',
                '${newFolder.title}',
                '${newFolder.description}',
                0, 0,
                ${_create_time}, 
                ${_create_time});`;

            parentFolder.newFolderArray.push(_id);
            parentFolder.folderIndexArray = parentFolder.newFolderArray.join(';');
            // oldFolder
            var upDateFolderIndexSql = `
                UPDATE folder SET 
                folder_index_array = '${parentFolder.folderIndexArray}',
                update_time = ${_create_time}
                WHERE folder_id = '${parentFolder.folderId}';`;

            try {
                await DB.creat(sql);
                await DB.update(upDateFolderIndexSql);
                newFolder.folderId = _id;
                newFolder.id = _id;
                newFolder.folderParentId = parentFolder.folderId;
                newFolder.type = 0;
                newFolder.updateTime = _create_time;
                newFolder.createTime = _create_time;
                return {
                    type: 'success',
                    res: newFolder,
                };
            } catch (error) {
                return makeError(error);
            }
        },
        updateFolderIndexByInto: async (updateObj) => {
            var time = new Date().getTime();

            var fromSql = `
                UPDATE folder SET 
                folder_index_array = '${updateObj.fromFolder.index}',
                update_time = ${time}
                WHERE folder_id = '${updateObj.fromFolder.id}';`;
            var moveSql = `
                UPDATE folder SET 
                update_time = ${time},
                folder_parent_id = '${updateObj.moveFolder.parent}'
                WHERE folder_id = '${updateObj.moveFolder.id}';`;
            var toSql = `
                UPDATE folder SET 
                folder_index_array = '${updateObj.toFolder.index}',
                update_time = ${time}
                WHERE folder_id = '${updateObj.toFolder.id}';`;
            try {
                await DB.update(fromSql);
                await DB.update(moveSql);
                await DB.update(toSql);
                return {
                    type: 'success',
                    res: null,
                };
            } catch (error) {
                return makeError(error);
            };
        },
        updateFolderIndexBySame: async (updateObj) => {
            var time = new Date().getTime();
            var Sql = `
                UPDATE folder SET 
                folder_index_array = '${updateObj.index}',
                update_time = ${time}
                WHERE folder_id = '${updateObj.id}';`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: null,
                };
            } catch (error) {
                return makeError(error);
            };
        },
        updateFolderMessage: async (updateObj) => {
            var time = new Date().getTime();
            var Sql = `
            UPDATE folder SET 
            title = '${updateObj.title}',
            description = '${updateObj.description}',
            update_time = ${time}
            WHERE folder_id = '${updateObj.id}';`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: null,
                };
            } catch (error) {
                return makeError(error);
            };
        },
    },
    project: {
        getProjectDetail: async (id) => {
            try {
                var data = await DB.get(`SELECT * FROM project WHERE project_id = '${id}'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        getProjectList: async () => {
            try {
                var data = await DB.find(`SELECT * FROM project`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        getProjectUser: async (id) => {
            var sql = `SELECT a.* FROM user a, project_user b WHERE b.project_id = '${id}' and b.user_id = a.user_id`;
            try {
                var data = await DB.find(sql);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        updateProject: async (data) => {
            var _update_time = new Date().getTime();
            var Sql = `UPDATE project SET 
                title = '${data.title}',
                description = '${data.description}',
                update_time = ${_update_time}
                WHERE project_id = '${data.projectId}'`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            };
        },
        getProjectUserEditData: async (id) => {
            var RelevanceSql = `SELECT * FROM project_user WHERE project_id = '${id}'`;
            var userSql = `SELECT * FROM user`;
            try {
                return {
                    type: 'success',
                    res: {
                        relevance: await DB.find(RelevanceSql),
                        user: await DB.find(userSql),
                    },
                };
            } catch (error) {
                return makeError(error);
            };
        },
    },
    theme: {
        getTheme: async () => {
            try {
                var data = await DB.get(`SELECT * FROM option WHERE key = 'theme'`);
                return {
                    type: 'success',
                    res: data
                };
            } catch (error) {
                return makeError(error);
            }
        },
        setTheme: async (data) => {
            var Sql = `UPDATE option SET value = '${data}' WHERE key = 'theme'`;
            try {
                await DB.update(Sql);
                return {
                    type: 'success',
                    res: data,
                };
            } catch (error) {
                return makeError(error);
            };
        }
    },
    tools: tools,
}